import {
  EllipsisOutlined,
  PlusOutlined,
  AreaChartOutlined,
} from '@ant-design/icons';
import type { ActionType, ProColumns } from '@ant-design/pro-components';
import { ProTable } from '@ant-design/pro-components';
import { Button, Dropdown } from 'antd';
import { useRef } from 'react';
import { useState } from 'react';
import { timestampToTime } from '@/utils/index';
import { Switch } from 'antd';
import { Col, Row, Table, Tooltip } from 'antd';
import React from 'react';
import { history, connect } from 'umi';
type GithubIssueItem = {
  url: string;
  id: number;
  number: number;
  title: string;
  labels: {
    name: string;
    color: string;
  }[];
  state: string;
  comments: number;
  created_at: string;
  updated_at: string;
  closed_at?: string;
};

function businessCom(props: any) {
  const [data, setData] = useState(props.data);
  const actionRef = useRef<ActionType>();
  const [newData, setNewData] = useState([]);
  const columns: ProColumns<GithubIssueItem>[] = [
    {
      title: 'ID',
      dataIndex: 'id',
    },
    {
      title: '楼盘名称',
      dataIndex: 'title',
      render: (item, info: any) => {
        const img =
          info.rec_position == 1 ? (
            <img
              src="http://fangadmin.shbwyz.com/static/images/rec.png"
              alt=""
            />
          ) : (
            ''
          );
        return (
          <React.Fragment>
            {item}
            <Tooltip
              title={
                <img
                  style={{
                    width: '100%',
                  }}
                  src={`http://fangadmin.shbwyz.com${info.img}`}
                />
              }
              style={{
                margin: '0 6',
              }}
            >
              <AreaChartOutlined />
            </Tooltip>
            {img}
          </React.Fragment>
        );
      },
    },
    {
      title: '所属区域',
      dataIndex: 'address',
    },
    {
      title: '价格',
      key: 'price',
      dataIndex: 'price',
      render: (item: any) => {
        return item === 0 ? (
          <span>待定</span>
        ) : item < 10 ? (
          <span>{item}万/套</span>
        ) : (
          <span>{item}元/m²</span>
        );
      },
    },
    {
      title: '更新时间',
      dataIndex: 'update_time',
      render: (item) => {
        return timestampToTime(item);
      },
    },
    {
      title: '排序',
      dataIndex: 'ordid',
    },
    {
      title: '状态',
      dataIndex: 'status',
      render: (item: any) => {
        return (
          <Switch
            checkedChildren="开启"
            unCheckedChildren="关闭"
            defaultChecked={item == 0 ? false : true}
          />
        );
      },
    },
    {
      title: '操作',
      valueType: 'option',
      key: 'option',
      render: (text, record, _, action) => {
        return (
          <React.Fragment>
            <Row>
              <Col>
                <Button
                  style={{
                    color: '#fff',
                    background: '#009688',
                    marginRight: 5,
                    borderRadius: 5,
                    marginBottom: 5,
                  }}
                  size="small"
                  onClick={() => {
                    props.goDetail(record);
                    setData(
                      JSON.parse(localStorage.getItem('officeList') as string),
                    );
                  }}
                >
                  编辑
                </Button>
              </Col>
              <Col>
                <Button
                  style={{
                    color: '#fff',
                    background: '#FF5722',
                    marginRight: 5,
                    borderRadius: 5,
                    marginBottom: 5,
                  }}
                  size="small"
                  onClick={() => {
                    props.delList(record);
                    setData(
                      JSON.parse(localStorage.getItem('officeList') as string),
                    );
                  }}
                >
                  删除
                </Button>
              </Col>
            </Row>
          </React.Fragment>
        );
      },
    },
  ];
  const onKeyDown = (e: any) => {
    if (e.keyCode === 13) {
      const searchArr = data.filter((item: any) => {
        return item.title.includes(e.target.value);
      });
      setNewData(searchArr);
    }
  };
  const goAdd = () => {
    history.push('/house/business/addbusiness');
  };
  return (
    <div>
      <input type="text" onKeyDown={onKeyDown} />
      <br />
      <Button type="primary" danger onClick={goAdd}>
        新增
      </Button>
      <ProTable<GithubIssueItem>
        columns={columns}
        actionRef={actionRef}
        cardBordered
        rowSelection={{
          selections: [Table.SELECTION_ALL, Table.SELECTION_INVERT],
        }}
        dataSource={newData.length > 0 ? newData : data}
        editable={{
          type: 'multiple',
        }}
        columnsState={{
          persistenceKey: 'pro-table-singe-demos',
          persistenceType: 'localStorage',
        }}
        rowKey="id"
        search={false}
        options={{
          setting: {
            listsHeight: 400,
          },
        }}
        form={{
          // 由于配置了 transform，提交的参与与定义的不同这里需要转化一下
          syncToUrl: (values, type) => {
            if (type === 'get') {
              return {
                ...values,
                created_at: [values.startTime, values.endTime],
              };
            }
            return values;
          },
        }}
        pagination={{
          pageSize: 5,
          onChange: (page) => console.log(page),
        }}
        dateFormatter="string"
        headerTitle="高级表格"
        toolBarRender={() => [
          <Button key="button" icon={<PlusOutlined />} type="primary">
            新建
          </Button>,
          <Dropdown
            key="menu"
            menu={{
              items: [
                {
                  label: '1st item',
                  key: '1',
                },
                {
                  label: '2nd item',
                  key: '1',
                },
                {
                  label: '3rd item',
                  key: '1',
                },
              ],
            }}
          >
            <Button>
              <EllipsisOutlined />
            </Button>
          </Dropdown>,
        ]}
      />
    </div>
  );
}

export default businessCom;
